<template>
  <div class="widget-data">
    <el-form label-width="80px" size="mini">
      <el-form-item label="数据类型">
        <el-radio :label="0" v-model="widget.service.type">静态数据</el-radio>
        <el-radio :label="1" v-model="widget.service.type">接口数据</el-radio>
        <!-- <el-radio :label="2" v-model="widget.service.type">SQL数据</el-radio> -->
        <el-radio :label="3" v-model="widget.service.type">WebSocket</el-radio>
      </el-form-item>
      <!-- 静态数据 -->
      <template v-if="widget.service.type === 0">
        <el-form-item class="custom-form-item">
          <div class="prop-form-item-btn" @click="editWidgetData">编辑数据</div>
        </el-form-item>
      </template>
      <!-- 接口数据 -->
      <template v-if="widget.service.type === 1">
        <el-form-item label="接口地址">
          <el-input
            :rows="3"
            type="textarea"
            v-model="widget.service.api.url"
          ></el-input>
        </el-form-item>
        <el-form-item label="请求方式">
          <el-select
            v-model="widget.service.api.method"
            popper-class="black-select"
          >
            <el-option label="GET" value="GET"></el-option>
            <el-option label="POST" value="POST"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="请求频率">
          <el-input-number
            controls-position="right"
            v-model="widget.service.api.refresh"
          ></el-input-number>
        </el-form-item>
        <el-form-item class="custom-form-item">
          <div class="prop-form-item-btn" @click="viewApiData">查看数据</div>
        </el-form-item>
      </template>
      <!-- WebSocket -->
      <template v-if="widget.service.type === 3">
        <el-form-item label="组件ID">
          <span class="text-label">{{ widget.id }}</span>
          <el-popover
            popper-class="black-popover"
            placement="top-start"
            trigger="hover"
            content="组件ID会拼接到接口地址上，例如：ws://xxx.xxx.xxx/xxx/ID"
          >
            <i class="el-icon-info" slot="reference"></i>
          </el-popover>
        </el-form-item>
        <el-form-item label="接口地址">
          <el-input
            :rows="3"
            type="textarea"
            v-model="widget.service.websocket.url"
          ></el-input>
        </el-form-item>
      </template>
      <el-form-item class="custom-form-item">
        <div class="prop-form-item-btn" @click="refreshWidgetData">
          应用数据
        </div>
      </el-form-item>
    </el-form>
    <el-dialog
      title="编辑数据"
      width="50%"
      custom-class="props-dialog"
      :visible.sync="dialog.widgetData.visible"
      :modal="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="content">
        <code-mirror
          :ref="'ref-' + widget.id"
          :mirror="{
            code: JSON.stringify(this.widget.data, null, 4),
          }"
        ></code-mirror>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialog.widgetData.visible = false"
          >取 消</el-button
        >
        <el-button type="primary" size="small" @click="saveCodeInfo"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      title="查看数据"
      width="50%"
      custom-class="props-dialog"
      :visible.sync="dialog.apiData.visible"
      :modal="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="content">
        <code-mirror
          :ref="'ref-view-' + widget.id"
          :mirror="dialog.apiData.data"
        ></code-mirror>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialog.apiData.visible = false"
          >取 消</el-button
        >
        <el-button
          type="primary"
          size="small"
          @click="dialog.apiData.visible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";
import CodeMirror from "../part/CodeMirror.vue";
export default {
  props: {
    widget: {
      type: [Object],
      default: () => {},
    },
  },
  data() {
    return {
      dialog: {
        widgetData: {
          visible: false,
        },
        apiData: {
          visible: false,
          data: {
            code: "",
            readOnly: true,
          },
        },
      },
    };
  },
  methods: {
    editWidgetData() {
      if (this.$refs["ref-" + this.widget.id]) {
        // 解决CodeMirror值不刷新的问题
        setTimeout(() => {
          this.$refs["ref-" + this.widget.id].$refs.mirror.codemirror.refresh();
        }, 50);
      }
      this.dialog.widgetData.visible = true;
    },
    saveCodeInfo() {
      const code = this.$refs["ref-" + this.widget.id].mirror.code;
      this.widget.data = JSON.parse(code);
      this.dialog.widgetData.visible = false;
    },
    refreshWidgetData() {
      if (
        (this.widget.service.type === 1 && !this.widget.service.api.url) ||
        (this.widget.service.type === 3 && !this.widget.service.websocket.url)
      ) {
        this.$message({
          showClose: true,
          message: "未配置接口地址信息",
          type: "warning",
        });
        return;
      }
      // 通知组件刷新
      this.widget.service.timestamp = new Date().getTime();
    },
    viewApiData() {
      if (this.$refs["ref-view-" + this.widget.id]) {
        // 解决CodeMirror值不刷新的问题
        setTimeout(() => {
          this.$refs[
            "ref-view-" + this.widget.id
          ].$refs.mirror.codemirror.refresh();
        }, 50);
      }
      if (this.widget.service.api.url) {
        axios({
          method: this.widget.service.api.method,
          url: this.widget.service.api.url,
        })
          .then((response) => {
            this.dialog.apiData.data.code = JSON.stringify(
              response.data,
              null,
              4
            );
            this.dialog.apiData.visible = true;
          })
          .catch(() => {
            this.$message({
              showClose: true,
              message: "接口配置错误",
              type: "error",
            });
          });
      } else {
        this.dialog.apiData.visible = true;
      }
    },
  },
  components: {
    CodeMirror,
  },
};
</script>
<style lang="stylus">
.box-property {
  .widget-data {
    .el-form {
      .custom-form-item {
        .el-form-item__content {
          margin-left: 30px !important;

          .prop-form-item-btn {
            height: 30px;
            line-height: 30px;
            text-align: center;
            color: #2681ff;
            cursor: pointer;
            border: 1px solid #2681ff;
            user-select: none;

            &:hover {
              color: #ffffff;
              background-color: #2681ff;
            }

            &:focus, &:active {
              color: #ffffff;
              background-color: #66b1ff;
            }
          }
        }
      }
    }
  }
}

.widget-data {
  .el-form {
    .el-form-item {
      .el-form-item__label {
        width: 100px !important;
        color: #ffffff;
        padding-left: 25px;
        font-size: 12px;
        text-align: left;
        box-sizing: border-box;
      }

      .el-form-item__content {
        padding-right: 15px;
        margin-left: 100px !important;

        .el-input, .el-textarea {
          .el-input__inner, .el-textarea__inner {
            border-color: #000;
            border-radius: 0;
            color: #fff;
            background-color: #0f1014;
          }
        }

        .el-radio {
          width: 150px;
        }

        .el-popover__reference-wrapper {
          display: inline-block;
          margin-left: 10px;
          color: #409EFF;
          cursor: pointer;
        }

        .el-input-number {
          width: auto;

          .el-input-number__decrease, .el-input-number__increase {
            border-radius: 0 !important;
            border-color: #333 !important;
            background: transparent;

            i {
              color: #bcc9d4;
            }
          }
        }
      }
    }
  }

  .el-dialog__wrapper {
    z-index: 20002 !important;

    .props-dialog {
      background-color: #1b1e25;
      border: 1px solid #1b1e25;

      .el-dialog__header {
        .el-dialog__title {
          color: #fff;
        }
      }

      .el-dialog__body {
        padding: 10px 20px;

        .content {
          height: 300px;
          box-sizing: border-box;
        }
      }

      .el-dialog__footer {
        .dialog-footer {
          .el-button {
            &:hover {
              background-color: #000 !important;
            }
          }

          .el-button--default {
            color: #fff;
            border-color: #859094;
            background-color: transparent;
          }

          .el-button--primary {
            color: #409eff;
            border-color: #409eff;
            background-color: transparent;
          }
        }
      }
    }
  }
}
</style>